package com.example.pclimb.ui.review;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.example.pclimb.R;
import com.example.pclimb.databinding.ActivityCreateReviewBinding;
import com.example.pclimb.model.Review;
import com.example.pclimb.repository.UserRepository;
import com.example.pclimb.util.SharedPreferencesUtil;
import com.example.pclimb.viewmodel.ReviewViewModel;
import com.example.pclimb.viewmodel.UserViewModel;

/**
 * 创建评价页面
 */
public class CreateReviewActivity extends AppCompatActivity {

    private ActivityCreateReviewBinding binding;
    private ReviewViewModel reviewViewModel;
    private UserViewModel userViewModel;
    private int requestId;
    private int guideId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCreateReviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置工具栏
        setSupportActionBar(binding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        binding.toolbar.setNavigationOnClickListener(v -> onBackPressed());

        // 初始化ViewModel
        reviewViewModel = new ViewModelProvider(this).get(ReviewViewModel.class);
        userViewModel = new ViewModelProvider(this).get(UserViewModel.class);

        // 从Intent获取数据
        if (getIntent().hasExtra("request_id") && getIntent().hasExtra("guide_id")) {
            requestId = getIntent().getIntExtra("request_id", 0);
            guideId = getIntent().getIntExtra("guide_id", 0);
            
            if (requestId <= 0 || guideId <= 0) {
                Toast.makeText(this, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            
            // 加载向导信息
            loadGuideInfo();
        } else {
            Toast.makeText(this, R.string.error_loading_data, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 设置评价提交按钮
        binding.btnSubmit.setOnClickListener(v -> submitReview());
    }

    /**
     * 加载向导信息
     */
    private void loadGuideInfo() {
        binding.progressBar.setVisibility(View.VISIBLE);
        
        userViewModel.getUserById(guideId).observe(this, guide -> {
            binding.progressBar.setVisibility(View.GONE);
            
            if (guide != null) {
                binding.tvGuideName.setText(guide.getNickname());
            } else {
                Toast.makeText(this, R.string.error_loading_guide, Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    /**
     * 提交评价
     */
    private void submitReview() {
        // 获取评分和评论
        float rating = binding.ratingBar.getRating();
        String comment = binding.etComment.getText().toString().trim();
        
        // 验证输入
        if (rating <= 0) {
            Toast.makeText(this, R.string.error_rating_required, Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (TextUtils.isEmpty(comment)) {
            binding.tilComment.setError(getString(R.string.error_comment_required));
            return;
        }
        
        // 显示进度条
        binding.progressBar.setVisibility(View.VISIBLE);
        binding.btnSubmit.setEnabled(false);
        
        // 获取当前用户ID
        int userId = SharedPreferencesUtil.getUserId(this);
        
        // 创建评价对象
        Review review = new Review(userId, guideId, requestId, rating, comment);
        
        // 保存评价
        reviewViewModel.insert(review, new UserRepository.Callback<Long>() {
            @Override
            public void onResult(Long result) {
                runOnUiThread(() -> {
                    binding.progressBar.setVisibility(View.GONE);
                    binding.btnSubmit.setEnabled(true);
                    
                    if (result > 0) {
                        // 评价保存成功
                        Toast.makeText(CreateReviewActivity.this, 
                                R.string.review_success, Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        // 评价保存失败
                        Toast.makeText(CreateReviewActivity.this, 
                                R.string.review_failure, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
} 